package com.xin.di.uav.applet.service.impl;

import com.xin.di.uav.po.GoodsMapper;
import com.xin.di.uav.applet.request.GoodsAddParam;
import com.xin.di.uav.applet.request.GoodsEditParam;
import com.xin.di.uav.applet.resp.ProductResp;
import com.xin.di.uav.applet.service.GoodsService;
import com.xin.di.uav.common.exception.BusinessException;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.response.IdAndNameResp;
import com.xin.di.uav.common.utils.BeanUtil;
import com.xin.di.uav.operate.mapper.MerchantMapper;
import com.xin.di.uav.user.order.po.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public boolean add(GoodsAddParam goodsAddParam) {
        Product product = BeanUtil.copyProperties(goodsAddParam, Product.class);
        product.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        return goodsMapper.insert(product) > 0;
    }

    @Override
    public boolean delete(String id) {
        return goodsMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(GoodsEditParam goodsEditParam) {
        String id = goodsEditParam.getId();
        Product product = goodsMapper.selectById(id);
        if (product == null) {
            throw new BusinessException("记录不存在");
        }
        product = BeanUtil.copyProperties(goodsEditParam, Product.class);
        return goodsMapper.updateById(product) > 0;
    }

    @Override
    public boolean updateStatus(IdRequest idRequest) {
        String id = idRequest.getId();
        Product product = goodsMapper.selectById(id);
        if (product == null) {
            throw new BusinessException("记录不存在");
        }
        boolean status = product.isStatus();
        product.setStatus(!status);
        return goodsMapper.updateById(product) > 0;
    }

    @Override
    public Product getById(String id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public ProductResp list(int pageNum, int pageSize, String name) {
        pageNum = (pageNum - 1) * pageSize;
        ProductResp productResp = new ProductResp();
        int cnt = goodsMapper.count(name);
        if (cnt > 0) {
            productResp.setTotal(cnt);
            List<Product> products = goodsMapper.selectList(pageNum, pageSize, name);
            List<String> merchantIds = products.stream().map(Product::getMerchantId).toList();
            List<IdAndNameResp> idAndNameRespList = merchantMapper.selectIdAndName(merchantIds);
            List<ProductResp.Record> recordList = new ArrayList<>();
            for (Product product : products) {
                ProductResp.Record record = BeanUtil.copyProperties(product, ProductResp.Record.class);
                for (IdAndNameResp idAndNameResp : idAndNameRespList) {
                    if (idAndNameResp.getId().equals(product.getMerchantId())) {
                        record.setMerchantId(idAndNameResp.getId());
                        record.setMerchantName(idAndNameResp.getName());
                    }
                }

                recordList.add(record);
            }


            productResp.setRecordList(recordList);
        }


        return productResp;
    }
}
